import Vue from 'vue'
import store from '@/store'
import Router from 'vue-router'
import BreedingManagement from './BreedingManagement'
import Visualization from './Visualization'
import ProcessingManagement from './ProcessingManagement'
import BasicInformation from './BasicInformation'
import Source from './Source'
import User from './User'
import Login from './Login'

import { Message } from 'element-ui'


import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
Vue.use(Router)
const originalPush = Router.prototype.push
   Router.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

export const routes =  [
  {
    path: '/',
    redirect: '/login'
  },
  ...Login,
  ...BasicInformation,
  ...BreedingManagement,
  ...Visualization,
  ...ProcessingManagement,
  ...User,
  ...Source
]

const router = new Router({
  mode: 'hash',
  routes
})


//  把路径还有名字放进Worktab 这个是标签导航栏用到的
const notoken = ['/login','/forgetPassword','/register','/index','/list'] // 不需要tonken的页面
router.beforeEach((to, from, next) => {
  NProgress.start();
  if(notoken.indexOf(to.path) == -1) {
    const token = localStorage.getItem('token')
    if(token == null || token == '') {
      Message.info('您不在登录状态，请先登录!')
      router.replace('/login')
    }
  }
  store.dispatch('worktabRoute',{
    to: {
      name: to.meta.title,
      path: to.fullPath
    },
    from: {
      name: from.meta.title,
      path: from.fullPath
    }
  })
  next()
})
router.afterEach(() => {
  NProgress.done();
})

export default router
